package io.yizoo.system.mapper;

import java.util.Map;

public class sys_flowMapperProvider {
    public String GetUserIdByNodeInsIdAndNodeinsUser(){
        String sql = "" +
                " SELECT info.user_id,ei.name AS ei_name FROM ( " +
                "   SELECT user_id,person.nodeins_id " +
                "   FROM fw_flowins_node_user person " +
                "   WHERE person.user_type IN ('user') " +
                "   UNION " +
                "   SELECT uu.id AS user_id,person.nodeins_id " +
                "   FROM fw_flowins_node_user person " +
                "   INNER JOIN sys_user uu ON uu.dept_id = person.user_id " +
                "   WHERE person.user_type IN ('org') " +
                "   UNION " +
                "   SELECT rr.user_id,person.nodeins_id " +
                "   FROM fw_flowins_node_user person " +
                "   INNER JOIN sys_role_rel_user rr ON rr.role_id = person.user_id " +
                "   WHERE person.user_type IN ('role') " +
                "   UNION " +
                "   SELECT uu_fzr.id AS user_id,person.nodeins_id " +
                "   FROM fw_flowins_node_user person " +
                "   INNER JOIN sys_user uu ON uu.id = person.user_id " +
                "   INNER JOIN biz_hrm_ei_basic ei ON ei.emp_code = uu.emp_code " +
                "   INNER JOIN biz_hrm_ei_rel_org rr ON rr.ei_id = ei.id AND rr.pm = '1' " +
                "   INNER JOIN biz_hrm_ei_rel_org rr_fzr ON rr_fzr.org_id = rr.org_id AND rr_fzr.fzr='1' " +
                "   INNER JOIN biz_hrm_ei_basic ei_fzr ON ei_fzr.id = rr_fzr.ei_id " +
                "   INNER JOIN sys_user uu_fzr ON uu_fzr.emp_code = ei_fzr.emp_code " +
                "   WHERE person.user_type IN ('zjsj') " +
                " ) info " +
                " INNER JOIN sys_user uu ON uu.id = info.user_id " +
                " INNER JOIN biz_hrm_ei_basic ei ON ei.emp_code = uu.emp_code " +
                " WHERE info.nodeins_id = #{nodeins_id} " +
                "  " +
                "  " +
                "  " +
                "  " +
                "  ";
        return sql;
    }
    public String GetUserIdByNodeInsIdAndNodeinsUser_Zjsj(){
        String sql  = "" +
                " SELECT uu_fzr.id AS user_id,ei_fzr.name AS ei_name  " +
                " FROM (" + GetUserIdByNodeInsIdAndNodeinsUser() + ") info " +
                " INNER JOIN sys_user uu ON uu.id = info.user_id " +
                " INNER JOIN biz_hrm_ei_basic ei ON ei.emp_code = uu.emp_code " +
                " INNER JOIN biz_hrm_ei_rel_org rr ON rr.ei_id = ei.id AND rr.pm = '1' " +
                " INNER JOIN biz_hrm_ei_rel_org rr_fzr ON rr_fzr.org_id = rr.org_id AND rr_fzr.fzr='1' " +
                " INNER JOIN biz_hrm_ei_basic ei_fzr ON ei_fzr.id = rr_fzr.ei_id " +
                " INNER JOIN sys_user uu_fzr ON uu_fzr.emp_code = ei_fzr.emp_code " +
                "  " +
                "  ";
        return sql;
    }

    public String GetUserTaskRunning(){
        String sql = Sql_GetUserTaskRunning();
        sql = "SELECT * FROM ( " + sql + " ) info ORDER BY IFNULL(status_time,create_time) DESC ";
        sql = "SELECT * FROM ( " + sql + " ) info LIMIT #{min_index},#{index_num} ";
        return sql;
    }
    public String GetUserTaskRunningCount(){
        String sql = Sql_GetUserTaskRunning();
        sql = "SELECT COUNT(id) AS count FROM ( " + sql + " ) info";
        return sql;
    }
    private String Sql_GetUserTaskRunning(){
        String sql = "" +
                " SELECT 'flow_task' AS type,flow.code AS flow_code,yw.data_id AS id,nodeins.id AS nodeins_id,nodeins.title, " +
                " IFNULL(node.node_url,'/system/flow/one_road_flowsec_form_s') AS url, " +
                " date_format(nodeins.sys_createtime, '%Y-%m-%d %H:%i:%s') AS create_time, " +
                " person.status,date_format(person.status_time, '%Y-%m-%d %H:%i:%s') AS status_time,null as remark,flow.title AS flow_title,flowins.sys_code_key, " +
                " c_emp.name as create_emp_name " +
                " FROM fw_flowins_node_user person " +
                " INNER JOIN fw_flowins_node nodeins ON nodeins.id = person.nodeins_id AND nodeins.status = 'running' " +
                " INNER JOIN fw_flowins_yw yw ON yw.flow_id = nodeins.flowins_id " +
                " INNER JOIN fw_flow_node node ON node.id = nodeins.node_id " +
                " INNER JOIN fw_flowins flowins ON flowins.id = yw.flow_id " +
                " INNER JOIN fw_flow flow ON flow.id = flowins.flow_id " +
                " LEFT JOIN sys_user c_uu ON c_uu.id = flowins.sys_createuser " +
                " LEFT JOIN biz_hrm_ei_basic c_emp ON c_emp.emp_code = c_uu.emp_code " +
                " WHERE 1=1 " +
                " AND person.status = 'running' " +
                " AND ( " +
                "   1>1 " +
                "   OR (person.user_type='user' AND person.user_id = #{user_id}) " +
                "   OR (person.user_type='org' AND person.user_id IN (SELECT dept_id FROM sys_user WHERE id = #{user_id})) " +
                "   OR (person.user_type='role' AND person.user_id IN (SELECT role_id FROM sys_role_rel_user WHERE user_id = #{user_id})) " +
                "   OR (person.user_type='zjsj' AND person.user_id IN ( " +
                "     SELECT uu.id " +
                "     FROM ( " +
                "       SELECT rr.org_id,uu.id AS user_id FROM biz_hrm_ei_rel_org rr " +
                "       INNER JOIN biz_hrm_ei_basic fzr ON fzr.id = rr.ei_id " +
                "       INNER JOIN sys_user uu ON uu.emp_code = fzr.emp_code AND uu.status = '启用' " +
                "       WHERE rr.fzr=1 " +
                "     ) fzr " +
                "     INNER JOIN biz_hrm_ei_rel_org rr ON rr.org_id = fzr.org_id AND rr.pm = 1 " +
                "     INNER JOIN biz_hrm_ei_basic bb ON bb.id = rr.ei_id " +
                "     INNER JOIN sys_user uu ON uu.emp_code = bb.emp_code " +
                "     WHERE fzr.user_id = #{user_id} " +
                "   )) " +
                " ) " +
                " GROUP BY yw.data_id,nodeins.id " +
                " UNION " +
                " SELECT 'yw_task' AS type,rr.table_name AS flow_code,t.id,null AS nodeins_id,t.title,rr.url, " +
                " date_format(t.sys_createtime, '%Y-%m-%d %H:%i:%s') AS create_time,t.status,date_format(t.status_time, '%Y-%m-%d %H:%i:%s') AS status_time, " +
                " t.remark,'任务' AS flow_title,t.sys_code_key, " +
                " c_emp.name as create_emp_name " +
                " FROM fw_flowsec_extra_task t " +
                " INNER JOIN fw_flowsec_extra_task_rel rr ON rr.type = t.type " +
                " LEFT JOIN sys_user c_uu ON c_uu.id = t.sys_createuser " +
                " LEFT JOIN biz_hrm_ei_basic c_emp ON c_emp.emp_code = c_uu.emp_code " +
                " WHERE 1=1 " +
                " AND t.status = 'running' " +
                " AND ( " +
                "   1>1 " +
                "   OR (t.user_type='user' AND t.user_id = #{user_id}) " +
                "   OR (t.user_type='user' AND t.emp_code IN (SELECT emp_code FROM sys_user WHERE id = #{user_id})) " +
                "   OR (t.user_type='org' AND t.user_id IN (SELECT dept_id FROM sys_user WHERE id = #{user_id})) " +
                "   OR (t.user_type='role' AND t.user_id IN (SELECT role_id FROM sys_role_rel_user WHERE user_id = #{user_id})) " +
                " ) " +
                "  " +
                "  ";
        return sql;
    }

    public String GetUserTaskCompleted(){
        String sql = Sql_GetUserTaskCompleted();
        sql = "SELECT * FROM ( " + sql + " ) info ORDER BY IFNULL(status_time,sys_createtime) DESC ";
        sql = "SELECT * FROM ( " + sql + " ) info LIMIT #{min_index},#{index_num} ";
        return sql;
    }
    public String GetUserTaskCompletedCount(){
        String sql = Sql_GetUserTaskCompleted();
        sql = "SELECT COUNT(id) AS count FROM ( " + sql + " ) info";
        return sql;
    }
    private String Sql_GetUserTaskCompleted(){
        String sql = "" +
                " SELECT 'flow_task' AS type,flow.code AS flow_code,yw.data_id AS id,gg.nodeins_id,nodeins.title " +
                " ,nodeins.status as nodeins_status,'completed' as preson_status " +
                " ,yw.data_id " +
                " ,IFNULL(node.node_url,'/system/flow/one_road_flowsec_form_s') AS url " +
                " ,date_format(nodeins.sys_createtime, '%Y-%m-%d %H:%i:%s') AS create_time " +
                " ,nodeins.sys_createtime,date_format(gg.status_time, '%Y-%m-%d %H:%i:%s') AS status_time,flow.title AS flow_title,flowins.sys_code_key " +
                " ,c_emp.name as create_emp_name " +
                " FROM (SELECT nodeins_id,MAX(status_time) AS status_time,status_user FROM fw_flowins_node_log WHERE status IN ('completed','submit_no_change') GROUP BY nodeins_id) gg " +
                " INNER JOIN fw_flowins_node nodeins ON nodeins.id = gg.nodeins_id " +
                " INNER JOIN fw_flowins_yw yw ON yw.flow_id = nodeins.flowins_id " +
                " INNER JOIN fw_flowins flowins ON flowins.id = yw.flow_id " +
                " INNER JOIN fw_flow flow ON flow.id = flowins.flow_id " +
                " INNER JOIN fw_flow_node node ON node.id = nodeins.node_id " +
                " LEFT JOIN sys_user c_uu ON c_uu.id = flowins.sys_createuser " +
                " LEFT JOIN biz_hrm_ei_basic c_emp ON c_emp.emp_code = c_uu.emp_code " +
                " WHERE 1=1 AND gg.status_user = #{user_id} " +
                " GROUP BY yw.data_id,nodeins.id " +
                " UNION " +
                " SELECT 'yw_task' AS type,rr.table_name AS flow_code,t.id,null AS nodeins_id,t.title, " +
                " t.status,t.remark,t.data_id,rr.url, " +
                " date_format(t.sys_createtime, '%Y-%m-%d %H:%i:%s') AS create_time, " +
                " t.sys_createtime,date_format(t.status_time, '%Y-%m-%d %H:%i:%s') AS status_time,'任务' AS flow_title,t.sys_code_key, " +
                " c_emp.name as create_emp_name " +
                " FROM fw_flowsec_extra_task t " +
                " INNER JOIN fw_flowsec_extra_task_rel rr ON rr.type = t.type " +
                " LEFT JOIN sys_user c_uu ON c_uu.id = t.sys_createuser " +
                " LEFT JOIN biz_hrm_ei_basic c_emp ON c_emp.emp_code = c_uu.emp_code " +
                " WHERE 1=1 " +
                " AND t.status = 'completed' " +
                " AND ( " +
                "   1>1 " +
                "   OR (t.user_type='user' AND t.user_id = #{user_id}) " +
                "   OR (t.user_type='user' AND t.emp_code IN (SELECT emp_code FROM sys_user WHERE id = #{user_id})) " +
                "   OR (t.user_type='org' AND t.user_id IN (SELECT dept_id FROM sys_user WHERE id = #{user_id})) " +
                "   OR (t.user_type='role' AND t.user_id IN (SELECT role_id FROM sys_role_rel_user WHERE user_id = #{user_id})) " +
                " ) " +
                "  " +
                "  ";
        return sql;
    }

    public String GetUserTask(){
        String sql = Sql_GetUserTask();
        sql = "SELECT * FROM ( " + sql + " ) info ORDER BY IFNULL(status_time,sys_createtime) DESC ";
        sql = "SELECT * FROM ( " + sql + " ) info LIMIT #{min_index},#{index_num} ";
        return sql;
    }
    public String GetUserTaskCount(){
        String sql = Sql_GetUserTask();
        sql = "SELECT COUNT(id) AS count FROM ( " + sql + " ) info";
        return sql;
    }
    private String Sql_GetUserTask(){
        String sql = "" +
                " SELECT nodeins.id as nodeins_id,nodeins.title as nodeins_title " +
                " ,nodeins.status as nodeins_status,person.status as preson_status " +
                " ,yw.data_id,node.node_url AS url " +
                " ,nodeins.sys_createtime,person.status_time,flowins.sys_code_key,flowins.status AS flow_status  " +
                " FROM fw_flowins_node_user person " +
                " INNER JOIN fw_flowins_node nodeins ON nodeins.id = person.nodeins_id AND nodeins.status = 'running' " +
                " INNER JOIN fw_flowins_yw yw ON yw.flow_id = nodeins.flowins_id " +
                "    AND yw.flow_code= #{flow_code} " +
                "    AND ( #{data_id}='nothing' OR INSTR(#{data_id}, CONCAT(CONCAT(',',yw.data_id),',')) > 0 ) " +
                " INNER JOIN fw_flow_node node ON node.id = nodeins.node_id " +
                " INNER JOIN fw_flowins flowins ON flowins.id = yw.flow_id " +
                " WHERE 1=1 AND person.status = 'running' " +
                " AND ( " +
                "   1>1 " +
                "   OR (person.user_type='user' AND person.user_id = #{user_id}) " +
                "   OR (person.user_type='org' AND person.user_id IN (SELECT dept_id FROM sys_user WHERE id = #{user_id})) " +
                "   OR (person.user_type='role' AND person.user_id IN (SELECT role_id FROM sys_role_rel_user WHERE user_id = #{user_id})) " +
                "   OR (person.user_type='zjsj' AND person.user_id IN ( " +
                "     SELECT uu.id " +
                "     FROM ( " +
                "       SELECT rr.org_id,uu.id AS user_id FROM biz_hrm_ei_rel_org rr " +
                "       INNER JOIN biz_hrm_ei_basic fzr ON fzr.id = rr.ei_id " +
                "       INNER JOIN sys_user uu ON uu.emp_code = fzr.emp_code AND uu.status = '启用' " +
                "       WHERE rr.fzr=1 " +
                "     ) fzr " +
                "     INNER JOIN biz_hrm_ei_rel_org rr ON rr.org_id = fzr.org_id AND rr.pm = 1 " +
                "     INNER JOIN biz_hrm_ei_basic bb ON bb.id = rr.ei_id " +
                "     INNER JOIN sys_user uu ON uu.emp_code = bb.emp_code " +
                "     WHERE fzr.user_id = #{user_id} " +
                "   )) " +
                " ) " +
                " GROUP BY yw.data_id,nodeins.id " +
                " UNION " +
                " SELECT gg.nodeins_id,nodeins.title nodeins_title " +
                " ,nodeins.status as nodeins_status,'completed' as preson_status " +
                " ,yw.data_id,node.node_url AS url " +
                " ,nodeins.sys_createtime,gg.status_time,flowins.sys_code_key,flowins.status AS flow_status " +
                " FROM fw_flowins_node_log gg " +
                " INNER JOIN fw_flowins_node nodeins ON nodeins.id = gg.nodeins_id " +
                " INNER JOIN fw_flowins_yw yw ON yw.flow_id = nodeins.flowins_id " +
                " INNER JOIN fw_flow_node node ON node.id = nodeins.node_id " +
                " INNER JOIN fw_flowins flowins ON flowins.id = yw.flow_id " +
                " WHERE 1=1 AND gg.status IN ('completed','submit_no_change') AND gg.status_user = #{user_id} " +
                " GROUP BY yw.data_id,nodeins.id " +
                "  " +
                "  ";
        return sql;
    }

    public String SearchNodeinsPerson(){
        String sql = Sql_GetAllUserTask();
        sql = "SELECT person_id AS id,user_id,preson_status AS status FROM ( " + sql + " ) info WHERE nodeins_id=#{nodeins_id}";
        return sql;
    }
    public static String Sql_GetAllUserTask() {
        String sql = "" +
                " SELECT info.user_id,info.nodeins_id,info.preson_status,info.person_id " +
                " ,nodeins.title as nodeins_title,nodeins.flowins_id " +
                " ,nodeins.status as nodeins_status,yw.data_id,yw.flow_code,flowins.sys_code_key " +
                " FROM ( " +
                " SELECT person.user_id,person.nodeins_id,person.status as preson_status,person.id as person_id " +
                " FROM fw_flowins_node_user person WHERE person.user_type='user' " +
                " UNION " +
                " SELECT uu.id AS user_id,person.nodeins_id,person.status as preson_status,person.id as preson_id " +
                " FROM fw_flowins_node_user person  " +
                " INNER JOIN sys_org oo ON oo.id = person.user_id " +
                " INNER JOIN sys_user uu ON uu.dept_id = oo.id " +
                " WHERE person.user_type='org' " +
                " UNION " +
                " SELECT uu.id AS user_id,person.nodeins_id,person.status as preson_status,person.id as preson_id " +
                " FROM fw_flowins_node_user person " +
                " INNER JOIN sys_role_rel_user rr ON rr.role_id = person.user_id " +
                " INNER JOIN sys_user uu ON uu.id = rr.user_id " +
                " WHERE person.user_type='role' " +
                " UNION " +
                " SELECT uu_fzr.id AS user_id,person.nodeins_id,person.status as preson_status,person.id as preson_id " +
                " FROM fw_flowins_node_user person " +
                " INNER JOIN sys_user uu ON uu.id = person.user_id " +
                " INNER JOIN biz_hrm_ei_basic bb ON bb.emp_code = uu.emp_code " +
                " INNER JOIN biz_hrm_ei_rel_org rr ON rr.ei_id = bb.id AND rr.pm = 1 " +
                " INNER JOIN biz_hrm_ei_rel_org rr_fzr ON rr_fzr.org_id = rr.org_id AND rr_fzr.fzr = 1 " +
                " INNER JOIN biz_hrm_ei_basic bb_fzr ON bb_fzr.id = rr_fzr.ei_id " +
                " INNER JOIN sys_user uu_fzr ON uu_fzr.emp_code = bb_fzr.emp_code " +
                " WHERE person.user_type='zjsj' " +
                " ) info " +
                " INNER JOIN fw_flowins_node nodeins ON nodeins.id = info.nodeins_id " +
                " INNER JOIN fw_flowins_yw yw ON yw.flow_id = nodeins.flowins_id " +
                " INNER JOIN fw_flowins flowins ON flowins.id = yw.flow_id " +
                "  " +
                "  ";
        return sql;
    }

    public String GetFlowinsInfo_InitByUser(){
        String sql = Sql_GetFlowinsInfo_InitByUser();
        sql = "SELECT * FROM ( " + sql + " ) info ORDER BY sys_createtimestamp DESC ";
        sql = "SELECT * FROM ( " + sql + " ) info LIMIT #{min_index},#{index_num} ";
        return sql;
    }
    public String GetFlowinsInfo_InitByUser_Count(){
        String sql = Sql_GetFlowinsInfo_InitByUser();
        sql = "SELECT COUNT(id) AS count FROM ( " + sql + " ) info";
        return sql;
    }
    private String Sql_GetFlowinsInfo_InitByUser(){
        String sql = "" +
                " SELECT yw.data_id AS id,flow.code AS flow_code " +
                " ,flowins.title,flowins.status,flowins.sys_code_key " +
                " ,date_format(flowins.sys_createtime, '%Y-%m-%d %H:%i:%s') AS sys_createtime,flowins.sys_createtimestamp " +
                " ,uu.nick_name AS create_user_name " +
                " ,nodeins.id AS nodeins_id,nodeins.title AS nodeins_title,nodeins.status AS nodeins_status " +
                "  ,IFNULL(node.node_url,'/system/flow/one_road_flowsec_form_s') AS url " +
                " FROM fw_flowins flowins " +
                " INNER JOIN fw_flowins_yw yw ON yw.flow_id = flowins.id " +
                " INNER JOIN fw_flow flow ON flow.id = flowins.flow_id " +
                " INNER JOIN sys_user uu ON uu.id = flowins.sys_createuser " +
                " INNER JOIN ( " +
                "   SELECT id,flowins_id,title,status,node_id " +
                "   ,ROW_NUMBER() OVER (PARTITION BY flowins_id ORDER BY sys_createtimestamp desc) as rn " +
                "   FROM fw_flowins_node " +
                "   WHERE node_type = 'normal' " +
                " ) nodeins ON nodeins.flowins_id = flowins.id AND nodeins.rn=1 " +
                " INNER JOIN fw_flow_node node ON node.id = nodeins.node_id " +
                " WHERE flowins.sys_createuser = #{create_user_id} " +
                "  " +
                "  ";
        return sql;
    }

    public String FlowSecFormTable_Select_GetTask(Map<String,Object> factor){
        String sql = Sql_FlowSecFormTable_Select_GetTask(factor);
        sql = "SELECT * FROM ( " + sql + " ) info ORDER BY info.s_num,info.sys_createtime DESC ";
        sql = "SELECT * FROM ( " + sql + " ) info LIMIT #{min_index},#{index_num} ";
        return sql;
    }
    public String FlowSecFormTable_Select_GetTaskCount(Map<String,Object> factor){
        String sql = Sql_FlowSecFormTable_Select_GetTask(factor);
        sql = "SELECT COUNT(id) AS count FROM ( " + sql + " ) info";
        return sql;
    }
    public String Sql_FlowSecFormTable_Select_GetTask(Map<String,Object> factor){
        String condition = "";
        if(isMapHasItemNoEmpty(factor,"id")){
            condition = condition+" AND info.id = #{id}";
        }
        if(isMapHasItemNoEmpty(factor,"create_user_id")){
            condition = condition+" AND info.sys_createuser = #{create_user_id}";
        }
        for(int i=1;i<30;i++){
            String col_name = "col_"+i;
            if(isMapHasItemNoEmpty(factor,col_name)){
                condition = condition+" AND info."+col_name+" LIKE CONCAT('%',#{"+col_name+"},'%')";
            }
        }
        String sql = "" +
                "  " +
                " SELECT info.* " +
                " ,nodeins.title AS last_nodeins_title " +
                " FROM ( " +
                "   SELECT info.*,uu.nick_name AS create_user_name,task.flowins_id,task.sys_code_key " +
                "   ,ROW_NUMBER() OVER (PARTITION BY info.id ORDER BY task.s_num) as rn,task.s_num " +
                "   FROM ${table_name} info " +
                "   INNER JOIN sys_user uu ON uu.id = info.sys_createuser" +
                "   INNER JOIN ( " +
                "     SELECT flowins_id,data_id,sys_code_key,CASE WHEN nodeins_status='running' AND preson_status='running' THEN 1 ELSE 2 END AS s_num" +
                "     FROM (" + Sql_GetAllUserTask() + ") info " +
                "     WHERE info.flow_code = #{flow_code} AND info.user_id = #{user_id}" +
                "   ) task ON task.data_id = info.id " +
                " ) info" +
                " LEFT JOIN ( " +
                "   SELECT id,flowins_id,title,status,node_id " +
                "   ,ROW_NUMBER() OVER (PARTITION BY flowins_id ORDER BY sys_createtimestamp desc) as rn " +
                "   FROM fw_flowins_node " +
                "   WHERE node_type = 'normal' " +
                " ) nodeins ON nodeins.flowins_id = info.flowins_id AND nodeins.rn=1 " +
                " WHERE info.rn= 1 " +
                condition +
                "  " +
                "  ";
        return sql;
    }

    public String FlowSecFormTable_SelectByFlowinsId(Map<String,Object> factor){
        String condition = "";
        if(isMapHasItemNoEmpty(factor,"flowins_id")){
            condition = condition+" AND yw.flow_id = #{flowins_id}";
        }
        String sql = "" +
                " SELECT info.* " +
                " FROM ${table_name} info " +
                " LEFT JOIN sys_user uu ON uu.id = info.sys_createuser " +
                " LEFT JOIN fw_flowins_yw yw ON yw.data_id = info.id " +
                " WHERE 1=1 " +
                condition +
                "  " +
                "  ";
        return sql;
    }

    public String FlowSecFormTable_Select(Map<String,Object> factor){
        String sql = Sql_FlowSecFormTable_Select(factor);
        sql = "SELECT * FROM ( " + sql + " ) info ORDER BY info.sys_createtime DESC ";
        sql = "SELECT * FROM ( " + sql + " ) info LIMIT #{min_index},#{index_num} ";
        return sql;
    }
    public String FlowSecFormTable_Select_Count(Map<String,Object> factor){
        String sql = Sql_FlowSecFormTable_Select(factor);
        sql = "SELECT COUNT(id) AS count FROM ( " + sql + " ) info";
        return sql;
    }
    public String Sql_FlowSecFormTable_Select(Map<String,Object> factor){
        String condition = "";
        if(isMapHasItemNoEmpty(factor,"id")){
            condition = condition+" AND info.id = #{id}";
        }
        if(isMapHasItemNoEmpty(factor,"create_user_id")){
            condition = condition+" AND uu.id = #{create_user_id}";
        }
        for(int i=1;i<30;i++){
            String col_name = "col_"+i;
            if(isMapHasItemNoEmpty(factor,col_name)){
                condition = condition+" AND info."+col_name+" LIKE CONCAT('%',#{"+col_name+"},'%')";
            }
        }
        String sql = "" +
                " SELECT info.*,uu.nick_name AS create_user_name,flowins.status as flowins_status,flowins.sys_code_key " +
                " ,nodeins.title AS last_nodeins_title " +
                " FROM ${table_name} info " +
                " LEFT JOIN sys_user uu ON uu.id = info.sys_createuser " +
                " LEFT JOIN fw_flowins_yw yw ON yw.data_id = info.id AND yw.flow_code = #{flow_code} " +
                " LEFT JOIN fw_flowins flowins ON flowins.id = yw.flow_id " +
                " LEFT JOIN ( " +
                "   SELECT id,flowins_id,title,status,node_id " +
                "   ,ROW_NUMBER() OVER (PARTITION BY flowins_id ORDER BY sys_createtimestamp desc) as rn " +
                "   FROM fw_flowins_node " +
                "   WHERE node_type = 'normal' " +
                " ) nodeins ON nodeins.flowins_id = flowins.id AND nodeins.rn=1 " +
                " WHERE 1=1 " +
                condition +
                "  " +
                "  ";
        return sql;
    }
    public String FlowSecFormTable_Delete(Map<String,Object> factor){
        String sql = " DELETE FROM ${table_name} info WHERE id = #{id} ";
        return sql;
    }

    public String FindTableByName(){
        String sql = " SELECT TABLE_NAME,TABLE_SCHEMA FROM (INFORMATION_SCHEMA.TABLES) WHERE TABLE_NAME = #{table_name} ";
        return sql;
    }
    public String DDL_CreateTable_FlowSecForm(){
        String sql = "" +
                " CREATE TABLE `${table_name}` ( " +
                "   `id` varchar(40) NOT NULL COMMENT '主键', " +
                "   `remark` varchar(200) DEFAULT NULL COMMENT '备注', " +
                "   `sys_createtime` datetime DEFAULT NOW() COMMENT '创建时间', " +
                "   `sys_createuser` varchar(40) DEFAULT NULL COMMENT '创建者', " +
                "   PRIMARY KEY (`id`) " +
                " )" +
                "  " +
                "  ";
        return sql;
    }
    public String DDL_AddTableColumn_FlowSecForm_100(){
        String sql = " ALTER TABLE ${table_name} ADD ${col_name} varchar(100) COMMENT #{title}";
        return sql;
    }
    public String DDL_AddTableColumn_FlowSecForm_1000(){
        String sql = " ALTER TABLE ${table_name} ADD ${col_name} varchar(1000) COMMENT #{title}";
        return sql;
    }
    public String DDL_ChangeTableColumnType_FlowSecForm_100(){
        String sql = " ALTER TABLE ${table_name} MODIFY ${col_name} varchar(100) COMMENT #{title}";
        return sql;
    }
    public String DDL_ChangeTableColumnType_FlowSecForm_1000(){
        String sql = " ALTER TABLE ${table_name} MODIFY ${col_name} varchar(1000) COMMENT #{title}";
        return sql;
    }
    public String DDL_SetTableColumnComment_FlowSecForm(){
        String sql = " ALTER TABLE ${table_name} MODIFY ${col_name} ";
        return sql;
    }
    public String DDL_DropTableColumn_FlowSecForm(){
        String sql = " ALTER TABLE ${table_name} DROP COLUMN ${col_name} ";
        return sql;
    }
    public String DDL_DropTable_FlowSecForm(){
        String sql = " DROP TABLE ${table_name} ";
        return sql;
    }

    private boolean isMapHasItemNoEmpty(Map<String,Object> data,String name){
        return (data.containsKey(name) && data.get(name)!=null && !data.get(name).toString().equals(""));
    }

}
